package cn.loan.controller.api;

import cn.loan.config.key.StatusCode;
import cn.loan.service.UserService;
import cn.loan.tools.RamdomNumber;
import cn.loan.tools.message.SendSMS;
import cn.loan.tools.message.config.SmsTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * @Author: 毛苏宇
 * @Description: You can do it!
 * @Date: Create in 11:45 2019/3/21
 */
@RestController
@RequestMapping("/message")
public class SendSMSController {


    @Autowired
    private UserService userService;


    /**
     * 发送修改密码的短信
     *
     * @param phone   手机号
     * @param session
     * @return 返回是否发送成功的状态码
     */
    @PostMapping("/sendModifyPwdMsg")
    @ResponseBody
    public String sendModifyPwdMsg(String phone, HttpSession session) {
        try {
            //查询是否有该用户
            boolean flag = userService.userNumIsExits(phone);
            //如果有则进入if,否则直接return
            if (flag) {
                //随机生成的验证码
                String code = RamdomNumber.getRandomNum();
                //发送短信,并返回是否发送成功
                String to = SendSMS.SmsTo(phone, code, SmsTemplate.TEMPLATE_RESET_PWD_SMS);
                //如果成功
                if (StatusCode.STATUS_SUCCESS.equals(to)) {
                    //以手机号为key,存储在session中
                    session.setAttribute(phone, code);
                    return to;
                }
                //不管什么状态全部返回给前端,前端进行判断
                return to;
            }
            //用户不存在
            return StatusCode.STATUS_NOT_EXIST;
        } catch (Exception e) {
            e.printStackTrace();
            //返回发生异常的状态码
            return StatusCode.STATUS_ERROR;
        }
    }


    /**
     * 发送注册的短信
     * @param phone 手机号
     * @param session
     * @return 发送的状态
     */
    @PostMapping("/sendRegisterMsg")
    @ResponseBody
    public String sendRegisterMsg(String phone, HttpSession session) {
        System.out.println("手机号"+phone);
        try{
            boolean flag = userService.userNumIsExits(phone);   //查询是否有该用户
            //如果没有该用户,则发送短信
            if (!flag){
                String code = RamdomNumber.getRandomNum();   //随机生成的验证码
                String to = SendSMS.SmsTo(phone, code, SmsTemplate.TEMPLATE_REGISTER_SMS);   //发送短信,并返回是否发送成功
                //如果成功
                if (StatusCode.STATUS_SUCCESS.equals(to)) { //成功
                    session.setAttribute(phone, code); //以手机号为key,存储在session中
                    System.out.println(code);
                    return to;
                }
                return to;  //不管什么状态全部返回给前端,前端进行判断
            }
            return StatusCode.STATUS_EXIST; //用户存在
        }catch (Exception e){
            e.printStackTrace();
            return StatusCode.STATUS_ERROR; //返回发生异常的状态码
        }
    }

}
